package com.epfresh.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.Html;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.RequestEntityMap;
import com.epfresh.api.entity.RequestEntityPurchase;
import com.epfresh.api.entity.RequestTag;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.entity.StatusEntity;
import com.epfresh.api.helper.ApplicationHelper;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.api.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.MsgView;
import com.epfresh.api.widget.tablayout.CommonTabLayout;
import com.epfresh.api.widget.tablayout.TabEntity;
import com.epfresh.api.widget.tablayout.listener.OnTabSelectListener;
import com.epfresh.api.widget.tablayout.utils.UnreadMsgUtils;
import com.epfresh.bean.StoreDetail;
import com.epfresh.fragment.DcGoodsFragment;
import com.epfresh.fragment.DcStoreInfoFragment;
import com.epfresh.fragment.SecondKillFragment;
import com.epfresh.fragment.StoreHomeWebFragment;
import com.epfresh.fragment.WebFragment;
import com.epfresh.global.BaseActivity;
import com.epfresh.global.DataManager;
import com.epfresh.utils.CommonUtils;
import com.epfresh.views.CustomAlertDialog;
import com.epfresh.views.MultiStateView;
import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class DcStoreActivity extends BaseActivity implements DataManager.OnCartCountChangeListener, OnTabSelectListener {
    /**
     * 配送中心店铺
     * */

    private CommonTabLayout lyTab;

    private DcGoodsFragment dcGoodsFragment;//商品
    private DcStoreInfoFragment dcStoreInfoFragment;//店铺信息

    private WebFragment storeHomeWebFragment;
    private SecondKillFragment secondKillFragment;


//    private List<BaseFragment> fragments = new ArrayList<>();


    //店铺id
    private String storeId;

    private StoreDetail storeDetail;


    private String data;

    private MsgView msgView;


    private View vSetting,line_gray;
    private View vCart;
    private TextView tvDc,txt_contact;
    private ImageView ivSearch;
    //背景图片
    private ImageView ivBg,img_status_vip;
    private TextView tvToolTitleTips,txt_status;
    private TextView tvContact;
    private MultiStateView multi_state;
    private boolean isInit = true;


    public View getvCart() {
        return vCart;
    }

    public void setvCart(View vCart) {
        this.vCart = vCart;
    }

    public ViewGroup getMainView() {
        return (ViewGroup) findViewById(R.id.rl_main_layout);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dc_store);
        initView();
        if (savedInstanceState != null) {
            getIntent().putExtra("storeId", savedInstanceState.getString("storeId"));
            getIntent().putExtra("data", savedInstanceState.getString("data"));


        }
        storeId = getIntent().getStringExtra("storeId");
        DataManager.getInstance().registerMsgView("DcStoreActivity", this);

        data = getIntent().getStringExtra("data");

        if (data != null) {
            storeDetail = new Gson().fromJson(data, StoreDetail.class);
        }else {
            requestSingleDc(false,storeId);
        }

        if (storeDetail != null) {
            storeId = storeDetail.getId();
            loadData();
        }


        updateCount(DataManager.getInstance().getCartCnt());
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        if (dcGoodsFragment != null) {
            dcGoodsFragment.requestCount();
            dcGoodsFragment.updateGoodsCollect();
        }
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("storeId", storeId);
        outState.putString("data", data);
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        storeId = outState.getString("storeId");
    }

    protected View getToolBar() {

        return null;
    }

    protected void initToolbar() {

        ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        toolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        toolbarTitle.setOnClickListener(null);
        tvToolTitleTips = (TextView) findViewById(R.id.tv_title_tips);
        txt_status = findViewById(R.id.txt_status);
        txt_status.setOnClickListener(this);
        img_status_vip = findViewById(R.id.img_status_vip);
        tvContact = (TextView) findViewById(R.id.tv_contact);
        vSetting = findViewById(R.id.iv_more);
        vCart = findViewById(R.id.iv_cart);
        msgView = (MsgView) findViewById(R.id.msg);
        vSetting.setOnClickListener(this);
        vCart.setOnClickListener(this);
        ivSearch = (ImageView) findViewById(R.id.iv_search);
        ivBg = (ImageView) findViewById(R.id.iv_bg);
        ivSearch.setOnClickListener(this);
        tvDc = (TextView) findViewById(R.id.tv_change_store);
        tvDc.setOnClickListener(this);
        tvDc.setVisibility(View.GONE);
        txt_contact = findViewById(R.id.txt_contact);
        txt_contact.setOnClickListener(this);
        txt_contact.setVisibility(View.GONE);
        line_gray = findViewById(R.id.line_gray);
        line_gray.setVisibility(View.GONE);
        tvContact.setOnClickListener(this);
        toolbarTitle.setText("");
        tvToolTitleTips.setText("");
    }

    private void initView() {
        initToolbar();
        lyTab = (CommonTabLayout) findViewById(R.id.ly_navigation);
        lyTab.setTabItemRes(R.layout.layout_tab_dc);
        lyTab.setOnTabSelectListener(this);
        multi_state = findViewById(R.id.multi_state);
        multi_state.setViewForState(R.layout.loading_view, MultiStateView.VIEW_STATE_LOADING);

    }

    //0,首页，秒杀，商品，信息
    //1,首页，商品，信息
    //2,秒杀，商品，信息
    //3,商品，信息
    int typeNav = 0;
    String TAG_DC_GOODS_FRAGMENT = "TAG_DC_GOODS_FRAGMENT";
    String TAG_DC_INFO_FRAGMENT = "TAG_DC_INFO_FRAGMENT";
    String TAG_DC_HOME_FRAGMENT = "TAG_DC_HOME_FRAGMENT";
    String TAG_DC_SECKILL_FRAGMENT = "TAG_DC_SECKILL_FRAGMENT";

    private void updatePageStatus(int i) {
        FragmentManager fm = getSupportFragmentManager();

        FragmentTransaction transaction = fm.beginTransaction();
        if (storeHomeWebFragment != null) {
            transaction.hide(storeHomeWebFragment);
        }
        if (secondKillFragment != null) {
            transaction.hide(secondKillFragment);
        }

        if (dcGoodsFragment != null) {
            transaction.hide(dcGoodsFragment);
        }
        if (dcStoreInfoFragment != null) {
            transaction.hide(dcStoreInfoFragment);
        }

        if (typeNav == 0) {

            if (i == 2) {
                showDcGoodsFragment(transaction);
            } else if (i == 3) {
                showDcInfoFragment(transaction);
            } else if (i == 0) {
                showDcHomeFragment(transaction);
            } else if (i == 1) {
                showDcSecKillFragment(transaction);
            }
        } else if (typeNav == 1) {
            if (i == 1) {
                showDcGoodsFragment(transaction);
            } else if (i == 2) {

                showDcInfoFragment(transaction);
            } else if (i == 0) {
                showDcHomeFragment(transaction);
            }
        } else if (typeNav == 2) {
            if (i == 1) {
                showDcGoodsFragment(transaction);
            } else if (i == 2) {
                showDcInfoFragment(transaction);
            } else if (i == 0) {
                showDcSecKillFragment(transaction);
            }
        } else if (typeNav == 3) {
            if (i == 0) {
                showDcGoodsFragment(transaction);
            } else if (i == 1) {
                showDcInfoFragment(transaction);
            }
        }

        transaction.commit();
    }

    private void showDcHomeFragment(FragmentTransaction transaction) {
        if (storeHomeWebFragment == null) {
            Bundle mBundle = new Bundle();
            storeHomeWebFragment = new WebFragment(storeDetail.getHomepage(), storeId);
            mBundle.putString(StoreHomeWebFragment.URL_KEY, storeDetail.getHomepage());
            transaction.add(R.id.content, storeHomeWebFragment, TAG_DC_HOME_FRAGMENT);
        }
        transaction.show(storeHomeWebFragment);
    }

    private void showDcSecKillFragment(FragmentTransaction transaction) {
        if (secondKillFragment == null) {
            secondKillFragment = new SecondKillFragment(storeDetail.getId(), null, 0);
            transaction.add(R.id.content, secondKillFragment, TAG_DC_SECKILL_FRAGMENT);
        }
        transaction.show(secondKillFragment);
    }

    private void showDcGoodsFragment(FragmentTransaction transaction) {
        if (dcGoodsFragment == null) {
            dcGoodsFragment = new DcGoodsFragment();
            if (storeDetail != null) {
                Log.e("list", "list:" + storeDetail.getTopCategories());
                dcGoodsFragment.updateList(storeDetail.getTopCategories());
                dcGoodsFragment.setStoreDetail(storeDetail);
            }
            transaction.add(R.id.content, dcGoodsFragment, TAG_DC_GOODS_FRAGMENT);
        }
        transaction.show(dcGoodsFragment);
    }

    private void showDcInfoFragment(FragmentTransaction transaction) {
        if (dcStoreInfoFragment == null) {
            dcStoreInfoFragment = new DcStoreInfoFragment();
            if (storeDetail != null) {
                Log.e("list", "list:" + storeDetail.getTopCategories());
                dcStoreInfoFragment.updateData(storeDetail);
            }
            transaction.add(R.id.content, dcStoreInfoFragment, TAG_DC_INFO_FRAGMENT);
        }
        transaction.show(dcStoreInfoFragment);
    }

    private void loadData() {

//        String storeHomeUrl = storeDetail.getHomepage();
        boolean hasSeckill = storeDetail.isHasSeckill();
        boolean hasHome = storeDetail.isHomepage();
        if (!hasHome && !hasSeckill) {
            typeNav = 3;
        } else if (hasHome && hasSeckill) {
            typeNav = 0;
        } else if (!hasHome && hasSeckill) {
            typeNav = 2;
        } else if (hasHome && !hasSeckill) {
            typeNav = 1;
        }
        //0,首页，秒杀，商品，信息
        //1,首页，商品，信息
        //2,秒杀，商品，信息
        //3,商品，信息
        ArrayList<TabEntity> mTabEntities = new ArrayList<>();
        if (typeNav == 0) {

            mTabEntities.add(new TabEntity("店铺首页", R.mipmap.iv_store_home_select, R.mipmap.iv_store_home_unselect));
            mTabEntities.add(new TabEntity("秒杀", R.mipmap.iv_store_seckill_select, R.mipmap.iv_store_seckill_unselect));

        } else if (typeNav == 1) {
            mTabEntities.add(new TabEntity("店铺首页", R.mipmap.iv_store_home_select, R.mipmap.iv_store_home_unselect));

        } else if (typeNav == 2) {
            mTabEntities.add(new TabEntity("秒杀", R.mipmap.iv_store_seckill_select, R.mipmap.iv_store_seckill_unselect));
        } else if (typeNav == 3) {

        }
        mTabEntities.add(new TabEntity("商品", R.mipmap.iv_store_goods_select, R.mipmap.iv_store_goods_unselect));
        mTabEntities.add(new TabEntity("店铺信息", R.mipmap.iv_store_info_select, R.mipmap.iv_store_info_unselect));

//        for (int i = 0; i < mTitles.length; i++) {
//            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnSelectIds[i]));
//        }
        lyTab.setTabData(mTabEntities);
        //updateCount(DataManager.getInstance().getCartCnt());
        DataManager.getInstance().updateCartCnt();

        updatePageStatus(0);
        updateStoreInfo();
    }

    @Override
    public void onTabSelect(int i) {
        updatePageStatus(i);
    }

    @Override
    public void onTabReselect(int i) {

    }

    private void updateCount(int count) {
        UnreadMsgUtils.showLitter(msgView, count, false);
    }


    DisplayImageOptions options = new DisplayImageOptions.Builder().showImageOnLoading(R.mipmap.background_store) //设置图片在下载期间显示的图片
            .showImageForEmptyUri(R.mipmap.bg_toolbar_main)//设置图片Uri为空或是错误的时候显示的图片
            .showImageOnFail(R.mipmap.bg_toolbar_main)
            .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
            .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
            .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
            .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
            .bitmapConfig(Bitmap.Config.RGB_565).build();

    public static DisplayImageOptions getAvatarOptions(String expectTag) {
        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.store_logo) // 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.store_logo)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.store_logo) // 设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                .displayer(new FadeInBitmapDisplayer(300, true, false, false))// 是否图片加载好后渐入的动画时间
                .setExceptTag(expectTag)
                .build();// 构建完成
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
/*            case R.id.ll_like:
                if (storeDetail != null)
                    requestLike();
                break;*/
            case R.id.iv_search:
                MobclickAgent.onEvent(this, "Click_Search_BulkStore");/*统计点击*/
                if (storeDetail != null && storeDetail.getId() != null)
                    openPhone(storeDetail.getId());
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_tilte:
            case R.id.iv_logo:
                openInfoDialog();
                break;
            case R.id.iv_cart:

                intoCart();
                break;
            case R.id.iv_more:

                openMore();
                break;
            case R.id.tv_change_store:
                openMore();
                intoDc();
                break;
            case R.id.tv_contact:
//                String managerPhone = storeDetail.getManagerPhone();
//                if (managerPhone != null && !"".equals(managerPhone))
//                    CommonUtils.intoPhone(this, managerPhone);
                break;
            case R.id.txt_contact:
                openMore();
                String managerPhone = storeDetail.getManagerPhone();
                if (managerPhone != null && !"".equals(managerPhone))
                    CommonUtils.intoPhone(this, managerPhone);
                break;
            case R.id.txt_status:
                showAlertDialog(storeDetail.getName(),storeId);
                break;
        }
    }

    private void intoDc() {
        Intent tv_more = new Intent(this,
                MoreDeliveryCenterActivity.class);
        startActivity(tv_more);
    }


    private void openMore() {
        if (tvDc.getVisibility() == View.VISIBLE && txt_contact.getVisibility() == View.VISIBLE && line_gray.getVisibility() == View.VISIBLE) {
            txt_contact.setVisibility(View.GONE);
            line_gray.setVisibility(View.GONE);
            tvDc.setVisibility(View.GONE);
        } else {
            txt_contact.setVisibility(View.VISIBLE);
            line_gray.setVisibility(View.VISIBLE);
            tvDc.setVisibility(View.VISIBLE);
        }


    }

    private void intoCart() {
        if (ApplicationHelper.getInstance().getUser().getAccess_token() == null || "".equals(ApplicationHelper.getInstance().getUser().getAccess_token())) {
            Intent intent = new Intent(this, LoginActivity.class);
            intent.putExtra("relogin", true);
            startActivity(intent);
            return;
        }

        Intent intent = new Intent(this, CartActivity.class);
        intent.putExtra("home_tab_index", 2);
        startActivity(intent);
    }

//    DcInfoDialog dcInfoDialog;

    private void openInfoDialog() {
//        if (dcInfoDialog == null) {
//            dcInfoDialog = new DcInfoDialog(this);
//        }
//
//        if (dcInfoDialog.isShowing()) {
//            dcInfoDialog.dismiss();
//        } else {
//            dcInfoDialog.updateData(storeDetail);
//            dcInfoDialog.show();
//        }
        if (storeDetail != null) {
            Intent intent = new Intent(this, StoreDetailActivity.class);

            intent.putExtra("store_detail", new Gson().toJson(storeDetail));

            startActivity(intent);
        }
    }

    public void openPhone(String storeId) {
        Intent iv_search = new Intent(this,
                CenterSearchActivity.class);
        iv_search.putExtra("storeId", storeId);

        startActivity(iv_search);
    }

    public void updateStoreInfo() {
        if (storeDetail != null) {
//            tvMarketName.setText(storeDetail.getMarketShortName());


            if (storeDetail.getDiscountInfo() != null && !"".equals(storeDetail.getDiscountInfo())) {
                View tvDiscount = findViewById(R.id.tv_discount);
                tvDiscount.setVisibility(View.VISIBLE);
            }

//            toolbarTitle.setText(storeDetail.getName());
            toolbarTitle.setText(storeDetail.getName());
//            tvLikeCount.setText(storeDetail.getFans() + "");

//            initFlags(llTag, storeDetail.getTags());
            String businessTime = storeDetail.getBusinessTime();
            if ("".equals(businessTime)) {
                tvToolTitleTips.setText("");
//                findViewById(R.id.v_mid).setVisibility(View.GONE);
            } else {
                //tvToolTitleTips.setText("接单时间："+businessTime);
//                findViewById(R.id.v_mid).setVisibility(View.VISIBLE);
            }

            if (storeDetail.getMemberStatus() == 1) {
                img_status_vip.setVisibility(View.VISIBLE);
                txt_status.setVisibility(View.GONE);

            } else if (storeDetail.getMemberStatus() == 2) {
                txt_status.setVisibility(View.VISIBLE);
                txt_status.setText("申请中");
                txt_status.setTextColor(Color.parseColor("#ffffff"));
                txt_status.setClickable(false);
                img_status_vip.setVisibility(View.GONE);

            } else if (storeDetail.getMemberStatus() == 3) {
                txt_status.setVisibility(View.GONE);
                img_status_vip.setVisibility(View.GONE);

            }else if (storeDetail.getMemberStatus() == 4){
                txt_status.setVisibility(View.VISIBLE);
                txt_status.setText("重新申请");
                txt_status.setTextColor(Color.parseColor("#ffffff"));
                txt_status.setClickable(true);
                img_status_vip.setVisibility(View.GONE);

            } else if (storeDetail.getMemberStatus() == 5) {
                txt_status.setVisibility(View.VISIBLE);
                txt_status.setText("申请会员");
                txt_status.setTextColor(Color.parseColor("#ffffff"));
                txt_status.setClickable(true);
                img_status_vip.setVisibility(View.GONE);

            }


            updateImage();
        } else {
            toolbarTitle.setText("");
            tvToolTitleTips.setText("");
        }
    }

    public void updateImage() {
        if (storeDetail != null) {
            ImageLoader.getInstance().displayImage(storeDetail.getBackgroundImgUrl(), ivBg, options);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DataManager.getInstance().removeMsgView("DcStoreActivity");
    }


    public void requestDc() {
        RequestEntityMap re = new RequestEntityMap();
        re.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_store_dcStore);
        RequestTag requestTag = new RequestTag();
        requestTag.tag = com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_store_dcStore;
        requestTag.type = RequestTag.TYPE_CURRENT;
        requestTag.arg1 = "silent";
        re.setCityId(ApplicationHelper.getInstance().getCity().getCityId());
        ApplicationHelper.getInstance().requestSilent(re, requestTag, onDetailRequestListener);

    }


    OnRequestListener<StoreDetail> onDetailRequestListener = new OnRequestListener<StoreDetail>() {
        @Override
        public StoreDetail jsonToObj(String s) {
            return new Gson().fromJson(s, StoreDetail.class);
        }

        @Override
        public void onFail(int i, Object o, Object o1) {


        }

        @Override
        public void onResponseError(int i, Object o, Object o1) {

        }

        @Override
        public void onStart(Object o1) {
            if (o1 != null && o1 instanceof RequestTag) {
                RequestTag tag = (RequestTag) o1;
                if ("silent".equals(tag.arg1)) {

                } else {
                    showProgressDialog();
                }
            } else {
                showProgressDialog();
            }

        }

        @Override
        public void onResponse(ResponseEntity<StoreDetail> responseEntity, Object o1) {

            StoreDetail responseElement = responseEntity.getResponseElement();
            if (responseElement != null) {
                storeDetail = responseElement;
                updateStoreInfo();

            }


        }
    };


    @Override
    public void onBackPressed() {
//        if (dcInfoDialog != null && dcInfoDialog.isShowing()) {
//            dcInfoDialog.dismiss();
//        } else {
        super.onBackPressed();
//        }
    }

    @Override
    public void onCartCountChange(int count) {
        UnreadMsgUtils.showLitter(msgView, count, false);
    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent ev) {
//        if (ev.getAction() == MotionEvent.ACTION_DOWN && tvDc.getVisibility() == View.VISIBLE) {
//            float rawX = ev.getRawX();
//            float rawY = ev.getRawY();
//            //获取控件在屏幕的位置
//            int[] location = new int[2];
//            tvDc.getLocationOnScreen(location);
//            //控件相对于屏幕的x与y坐标
//            int x1 = location[0];
//            int y1 = location[1];
//
//            int x2 = tvDc.getRight() - tvDc.getLeft() + x1;
//            int y2 = tvDc.getBottom() - tvDc.getTop() + y1;
//
//
//            if (rawX >= x1 && rawX <= x2 && rawY >= y1 && rawY <= y2) {
//
//            } else {
//                openMore();
//                return true;
//            }
//        }
//        return super.dispatchTouchEvent(ev);
//    }

    //**************************************************************************************************************************************************
    public void requestSingleDc(boolean isSilent, String storeId) {
        RequestEntityMap re = new RequestEntityMap();
        re.putParameter("storeId", storeId);
        re.setCmd(com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_store_dcStore);
        RequestTag requestTag = new RequestTag();
        requestTag.tag = com.epfresh.api.constant.AppPurchaseRouterConstant.cmd_store_store_dcStore;
        if (isSilent) {
            requestTag.type = RequestTag.TYPE_CURRENT;
            requestTag.arg1 = "silent";
            re.setCityId(ApplicationHelper.getInstance().getCity().getCityId());
            ApplicationHelper.getInstance().requestSilent(re, requestTag, onDetailRequestListener2);
        } else {
            requestTag.arg1 = "1";
            request(re, requestTag, onDetailRequestListener2);
        }

    }


    OnRequestListener<StoreDetail> onDetailRequestListener2 = new OnRequestListener<StoreDetail>() {
        @Override
        public StoreDetail jsonToObj(String s) {
            return new Gson().fromJson(s, StoreDetail.class);
        }

        @Override
        public void onFail(int i, Object o, Object o1) {
            multi_state.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        }

        @Override
        public void onResponseError(int i, Object o, Object o1) {
            multi_state.setViewState(MultiStateView.VIEW_STATE_CONTENT);
        }

        @Override
        public void onStart(Object o1) {
            if (isInit) {
                multi_state.setViewState(MultiStateView.VIEW_STATE_LOADING);
                isInit = false;
            }
        }

        @Override
        public void onResponse(ResponseEntity<StoreDetail> responseEntity, Object o1) {
            multi_state.setViewState(MultiStateView.VIEW_STATE_CONTENT);
            StoreDetail responseElement = responseEntity.getResponseElement();
            if (responseElement != null) {
                storeDetail = responseElement;
            }

            if (storeDetail != null) {
                storeId = storeDetail.getId();
                loadData();
            }

        }
    };


    private void showAlertDialog(String strStoreName, final String storeId) {
        final CustomAlertDialog alertDialog = new CustomAlertDialog(this);
        alertDialog.setTitles(Html.fromHtml("您正在向<font color=\"#24AF75\">“" + strStoreName + "”</font>申请会员"))
                .setTitleColor("#313131")
                .setMessage("申请通过后可享受商户服务")
                .setMessageColor("#666666")
                .setNoVis(true)
                .setCancelOnTouchOutside(true)
                .setYesOnClickListener("确认", new CustomAlertDialog.YesOnClickListener() {
                    @Override
                    public void yesClick() {
                        applyVIP(storeId);
                        alertDialog.dismiss();
                    }
                })
                .setNoOnClickListener("取消", new CustomAlertDialog.NoOnClickListener() {
                    @Override
                    public void noClick() {
                        alertDialog.dismiss();
                    }
                })
                .show();
    }

    /**
     * 申请会员
     *
     * @param storeId
     */
    private void applyVIP(String storeId) {
        RequestEntityPurchase<Map<String, String>> data = new RequestEntityPurchase<>();
        data.setCmd(AppPurchaseRouterConstant.cmd_store_store_bind);
        Map<String, String> map = new HashMap<>();
        map.put("storeId", storeId);
        data.setParameters(map);

        request(data, AppPurchaseRouterConstant.cmd_store_store_bind, new OnRequestListener<StatusEntity>() {
            @Override
            public StatusEntity jsonToObj(String responseStr) {
                return new Gson().fromJson(responseStr, StatusEntity.class);
            }

            @Override
            public void onFail(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                T.toast(failCode + "");
            }

            @Override
            public void onResponseError(int failCode, Object arg, Object tag) {
                hideProgressDialog();
                T.toast(failCode + "");
            }

            @Override
            public void onStart(Object tag) {
                showProgressDialog();
            }

            @Override
            public void onResponse(ResponseEntity<StatusEntity> response, Object tag) {
                hideProgressDialog();
                StatusEntity responseElement = response.getResponseElement();
                if (responseElement.isSuccess()) {
                    refresh();
                } else {
                    T.toast("申请失败");
                }
            }
        });
    }

    private void refresh() {
        requestSingleDc(false,storeId);
    }

}
